/**
 * 
 */
package com.jm.web.form.table;

import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.servlet.jsp.JspWriter;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.BasePage;
import com.jm.web.data.BaseDataEntities;
import com.jm.web.data.BaseDataEntity;
import com.jm.web.form.support.FormItem;

/**
 * @author yanjie
 *
 * 2011/9/22 下午2:07:47
 */
public class Table extends FormItem{
	public final static String TAG_ITEMNAME = "table";
	public final static String TAG_VIEW = "view";
	public static final String TAG_TABLE_STYLE_NORMAL = "normal";
	public static final String TAG_TABLE_STYLE_FIEXEDROW = "fixedRow";
	public static final String TAG_TABLE_STYLE_FIEXEDROWANDCOLOUMN = "fixedRowCol";
	Map<String,String> widthMap = new HashMap<String,String>();//记录每一列的宽度
	Map<String,BaseDataEntity> colsMap = new HashMap<String,BaseDataEntity>();//记录每一列的宽度
	
	protected Thead thead = null ;
	protected Tbody tbody = null ;
	protected Tfoot tfoot = null ;
	protected String strView = "normal" ;
	
	
	@Override
	public void OnSetProperty(String strName, String strValue) {
		if(TAG_VIEW.equalsIgnoreCase(strName)){
			this.strView = strValue ;
			return ;
		}
		super.OnSetProperty(strName, strValue);
	}
	
	public void init() {
		this.setNodeName("table");
		super.init();
	}
	
	/**
	 * @param tag
	 */
	public Table() {}
	
	public Thead getThead() {
		return thead;
	}


	public void setThead(Thead thead) {
		this.thead = thead;
	}
	
	@Override
	public void onOutItemBegin(JspWriter writer) {
		super.onOutItemBegin(writer);
	}
	
	@Override
	public void onOutItemEnd(JspWriter writer) {
		super.onOutItemEnd(writer);
	}
	
	
	/**
	 * 加载表格模型
	 */
	public void loadColModelConfig(Thead head){
		if(head == null || head.getItems() == null)
			return ;
		
		Vector<String> cells = new Vector<String>();//记录已经处理过的单元格
		BaseDataEntities cols = new BaseDataEntities();//記錄表頭的config
		boolean bconfig = false;
		
		for (int i = 0; i < head.getItems().size(); i++) {
			Tr tr = (Tr)head.getItems().get(i);
			if(tr == null || tr.getItems() == null)
				continue;
			
			int rowIndex = i ;//第几行
			int colIndex = 0 ;//第几列
			for (int j = 0; j < tr.getItems().size(); j++) {
				Td td = (Td)tr.getItems().get(j);
				int colspan = td.getColspan();
				int rowspan = td.getRowspan();
				
				//处理当前行以下的行
				for (int k = 1; k < rowspan; k++) {
					for (int k2 = 0; k2 < colspan; k2++) {
						cells.add(StringHelper.Format("%1$s-%2$s",rowIndex+k, colIndex+k2));
					}
				}
				
				while(cells.contains(StringHelper.Format("%1$s-%2$s",rowIndex, colIndex))){
					colIndex ++ ;
				}
				
				if(colspan == 1){
					widthMap.put(StringHelper.getValue(colIndex), td.getWidth());
					if(!StringHelper.IsNullOrEmpty(td.GetExtValue("config","")))
						bconfig = true;
					
					BaseDataEntity col = td.getConfig("config");
					col.SetParamValue("colIndex",colIndex);
					cols.add(col);
					colsMap.put(col.gpsv("cid"), col);
					
					if(col.ContainesParam("editor") && !this.getBaseTag().getPage().isExport()){
						td.setNodeValue(StringHelper.Format("%1$s %2$s",
								"<img align=\"absmiddle\" alt=\"列支持行编辑\" src=\""+this.getBaseTag().getPage().getContextPath()+"/include/images/default/icon_rowedit2.png\" />",
							td.getNodeValue()	
							));
					}
				}
				
				for (int k3 = 0; k3 < colspan; k3++) {
					cells.add(StringHelper.Format("%1$s-%2$s",rowIndex, colIndex));
					colIndex ++ ; ;
				}
			}
		}
		
		if(bconfig){
			this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY,
				StringHelper.Format("$P.grid.config['%1$s']=%2$s;",
						this.getBaseTag().getId(),
						cols.TOJSONString()));
		}
	} 
	
	/**
	 * @return
	 * 输出表格列模型
	 */
	public String getColumnModel(){
		if(this.getBaseTag().getPage().isExport())
			return "";
		StringBuilderEx sb = new StringBuilderEx();
		for (int i = 0; i < widthMap.size(); i++) {
			sb.Append("<col width='%1$s'>",widthMap.get(StringHelper.getValue(i)));
		}
		return sb.toString();
	}
	
	public BaseDataEntity getColConfig(String cid){
		return colsMap.get(cid);
	}
	
	public String getView() {
		return strView;
	}
	
	public void setView(String strView) {
		this.strView = strView;
	}

	public Tbody getTbody() {
		return tbody;
	}

	public void setTbody(Tbody tbody) {
		this.tbody = tbody;
	}

	public Tfoot getTfoot() {
		return tfoot;
	}

	public void setTfoot(Tfoot tfoot) {
		this.tfoot = tfoot;
	}
}
